package com.zxsj.fdk.study.common.utils;

/**
 * @Description Thread Utilities
 * @Author zhengbaole
 * @Date 2020/12/22
 **/
public class ThreadUtils {

    private ThreadUtils() { }

    /**
     * @return 当前方法的调用者中是否包含某个特定的类
     *
     * @author zhengbaole
     * @lastEditor zhengbaole
     * @createTime 2020/12/22 4:09 下午
     * @editTime 2020/12/22 4:09 下午
     **/
    public static boolean invokersContains(Class<?> clz) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        for (StackTraceElement el : stackTrace) {
            String expectedName = clz.getName();
            if (el.getClassName().contains(expectedName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 勿在异步线程中调用，否则结果没有意义
     *
     * @return 当前线程是否来自请求的触发
     *
     * @author zhengbaole
     * @lastEditor zhengbaole
     * @createTime 2021/5/20 9:54 AM
     * @editTime 2021/5/20 9:54 AM
     */
    public static boolean isFromRequest() {
        try {
            String name = Thread.currentThread().getName();
            return name.contains("http-nio");
        } catch (Exception e) {
            return false;
        }
    }
}
